{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c13d1b70",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "# The Encoder--Decoder Architecture\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f6ad17a2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:29:03.809994Z",
     "iopub.status.busy": "2023-08-18T19:29:03.809520Z",
     "iopub.status.idle": "2023-08-18T19:29:06.566331Z",
     "shell.execute_reply": "2023-08-18T19:29:06.565200Z"
    },
    "origin_pos": 3,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "from torch import nn\n",
    "from d2l import torch as d2l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6442367c",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Encoder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bb981983",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:29:06.571309Z",
     "iopub.status.busy": "2023-08-18T19:29:06.570433Z",
     "iopub.status.idle": "2023-08-18T19:29:06.577664Z",
     "shell.execute_reply": "2023-08-18T19:29:06.576626Z"
    },
    "origin_pos": 8,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "class Encoder(nn.Module):  \n",
    "    \"\"\"The base encoder interface for the encoder--decoder architecture.\"\"\"\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "\n",
    "    def forward(self, X, *args):\n",
    "        raise NotImplementedError"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "895b4e28",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Decoder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7874f0f6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:29:06.581746Z",
     "iopub.status.busy": "2023-08-18T19:29:06.581040Z",
     "iopub.status.idle": "2023-08-18T19:29:06.587735Z",
     "shell.execute_reply": "2023-08-18T19:29:06.586641Z"
    },
    "origin_pos": 13,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "class Decoder(nn.Module):  \n",
    "    \"\"\"The base decoder interface for the encoder--decoder architecture.\"\"\"\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "\n",
    "    def init_state(self, enc_all_outputs, *args):\n",
    "        raise NotImplementedError\n",
    "\n",
    "    def forward(self, X, state):\n",
    "        raise NotImplementedError"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "609f3a78",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Putting the Encoder and Decoder Together"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a4223135",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:29:06.591527Z",
     "iopub.status.busy": "2023-08-18T19:29:06.590891Z",
     "iopub.status.idle": "2023-08-18T19:29:06.597930Z",
     "shell.execute_reply": "2023-08-18T19:29:06.596992Z"
    },
    "origin_pos": 17,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "class EncoderDecoder(d2l.Classifier):  \n",
    "    \"\"\"The base class for the encoder--decoder architecture.\"\"\"\n",
    "    def __init__(self, encoder, decoder):\n",
    "        super().__init__()\n",
    "        self.encoder = encoder\n",
    "        self.decoder = decoder\n",
    "\n",
    "    def forward(self, enc_X, dec_X, *args):\n",
    "        enc_all_outputs = self.encoder(enc_X, *args)\n",
    "        dec_state = self.decoder.init_state(enc_all_outputs, *args)\n",
    "        return self.decoder(dec_X, dec_state)[0]"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "language_info": {
   "name": "python"
  },
  "required_libs": [],
  "rise": {
   "autolaunch": true,
   "enable_chalkboard": true,
   "overlay": "<div class='my-top-right'><img height=80px src='http://d2l.ai/_static/logo-with-text.png'/></div><div class='my-top-left'></div>",
   "scroll": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}